﻿Imports System.Data.SqlClient

Public Class frmQLKHThueNha

    Private Sub frmQLKHThueNha_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listKhachHangThueNha As New DataTable()

        listKhachHangThueNha = KhachHangThueNhaBUS.GetAll()
        DataGridViewX1.DataSource = listKhachHangThueNha

        Dim manhathue As New DataTable()
        manhathue = ThongTinNhaBUS.GetAll()
        ComboBoxEx1.DataSource = manhathue
        ComboBoxEx1.DisplayMember = "MaNha"

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If Not txtHoTenKhachThue.Text = "" Then
            If Not txtPhone.Text = "" Then
                Dim sqlparameters(3) As SqlParameter
                Dim QLChuNha As KhachHangThueNhaDTO = New KhachHangThueNhaDTO()
                QLChuNha.strHoTen = txtHoTenKhachThue.Text
                QLChuNha.strSoDienThoai = txtPhone.Text
                QLChuNha.dtNgayThue = dtNgayThue.Value
                QLChuNha.iMaNhaThue = ComboBoxEx1.Text


                Dim para0 As New SqlParameter("hoten", SqlDbType.NVarChar)
                para0.Direction = ParameterDirection.Input
                para0.Value = QLChuNha.strHoTen
                sqlparameters(0) = para0

                Dim para1 As New SqlParameter("sodienthoai", SqlDbType.VarChar)
                para1.Direction = ParameterDirection.Input
                para1.Value = QLChuNha.strSoDienThoai
                sqlparameters(1) = para1

                Dim para2 As New SqlParameter("ngaythue", SqlDbType.Date)
                para2.Direction = ParameterDirection.Input
                para2.Value = QLChuNha.dtNgayThue
                sqlparameters(2) = para2

                Dim para3 As New SqlParameter("manhathue", SqlDbType.Int)
                para3.Direction = ParameterDirection.Input
                para3.Value = QLChuNha.iMaNhaThue
                sqlparameters(3) = para3


                KhachHangThueNhaBUS.Insert(sqlparameters)
                MessageBox.Show("Thêm Thành Công!!!!!")

            Else
                MsgBox("Bạn chưa nhập vào số điện thoại ", MsgBoxStyle.Critical, "Thông báo!")

                txtPhone.Text = Focus()

            End If
        Else


            MsgBox("Bạn chưa nhập vào họ tên ", MsgBoxStyle.Critical, "Thông báo!")

            txtHoTenKhachThue.Focus()
        End If


        Dim listKhachHangThueNha As New DataTable

        listKhachHangThueNha = KhachHangThueNhaBUS.GetAll()

        DataGridViewX1.DataSource = listKhachHangThueNha
    End Sub



    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If Not txtHoTenKhachThue.Text = "" Then
            If Not txtPhone.Text = "" Then
                Dim sqlparameters(4) As SqlParameter
                Dim QLChuNha As KhachHangThueNhaDTO = New KhachHangThueNhaDTO()

                QLChuNha.iMaKhachHangThueNha = DataGridViewX1.CurrentRow.Cells("makhachhangthuenha").Value
                QLChuNha.strHoTen = txtHoTenKhachThue.Text
                QLChuNha.strSoDienThoai = txtPhone.Text
                QLChuNha.dtNgayThue = dtNgayThue.Value
                QLChuNha.iMaNhaThue = ComboBoxEx1.Text

                Dim para0 As New SqlParameter("makhachhangthuenha", SqlDbType.Int)
                para0.Direction = ParameterDirection.Input
                para0.Value = QLChuNha.iMaKhachHangThueNha
                sqlparameters(0) = para0

                Dim para1 As New SqlParameter("hoten", SqlDbType.NVarChar)
                para1.Direction = ParameterDirection.Input
                para1.Value = QLChuNha.strHoTen
                sqlparameters(1) = para1

                Dim para2 As New SqlParameter("sodienthoai", SqlDbType.VarChar)
                para2.Direction = ParameterDirection.Input
                para2.Value = QLChuNha.strSoDienThoai
                sqlparameters(2) = para2

                Dim para3 As New SqlParameter("ngaythue", SqlDbType.Date)
                para3.Direction = ParameterDirection.Input
                para3.Value = QLChuNha.dtNgayThue
                sqlparameters(3) = para3



                Dim para4 As New SqlParameter("manhathue", SqlDbType.Int)
                para4.Direction = ParameterDirection.Input
                para4.Value = QLChuNha.iMaNhaThue
                sqlparameters(4) = para4


                KhachHangThueNhaBUS.Update(sqlparameters)
                MessageBox.Show("Sửa Thành Công!!!!!")





            Else
                MsgBox("Bạn chưa nhập vào số điện thoại ", MsgBoxStyle.Critical, "Thông báo!")

                txtPhone.Text = Focus()

            End If
        Else


            MsgBox("Bạn chưa nhập vào họ tên ", MsgBoxStyle.Critical, "Thông báo!")

            txtHoTenKhachThue.Focus()
        End If


        Dim listKhachHangThueNha As New DataTable

        listKhachHangThueNha = KhachHangThueNhaBUS.GetAll()

        DataGridViewX1.DataSource = listKhachHangThueNha
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim khachhangthuenha As KhachHangThueNhaDTO = New KhachHangThueNhaDTO()


        khachhangthuenha.iMaKhachHangThueNha = DataGridViewX1.CurrentRow.Cells("makhachhangthuenha").Value

        Dim para1 As New SqlParameter("makhachthue", SqlDbType.Int)
        para1.Direction = ParameterDirection.Input
        para1.Value = khachhangthuenha.iMaKhachHangThueNha

        KhachHangThueNhaBUS.Delete(para1)

        MessageBox.Show("Chúc mừng bạn xóa thành công!!!!!")
        Dim listKhachHangThueNha As New DataTable()

        listKhachHangThueNha = KhachHangThueNhaBUS.GetAll()
        DataGridViewX1.DataSource = listKhachHangThueNha
    End Sub



    Private Sub DataGridViewX1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewX1.SelectionChanged
        txtHoTenKhachThue.Text = DataGridViewX1.CurrentRow.Cells(1).Value
        txtPhone.Text = DataGridViewX1.CurrentRow.Cells(2).Value
        dtNgayThue.Text = DataGridViewX1.CurrentRow.Cells(3).Value
        ComboBoxEx1.Text = DataGridViewX1.CurrentRow.Cells(4).Value
    End Sub


End Class